package com.rxyb.upms.gate.controller;


import com.rxyb.core.respone.R;
import com.rxyb.security.annotation.Inner;
import com.rxyb.upms.gate.pojo.LoginReq;
import com.rxyb.upms.gate.pojo.LoginRsp;
import com.rxyb.upms.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YJH
 * @since 2020-05-26
 */
@RestController
@Slf4j
@RequestMapping("/sysUser/")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @Autowired(required = false)
    @Qualifier("consumerTokenServices")
    private ConsumerTokenServices consumerTokenServices;

    /**
     * 用户登录信息
     *
     * @param req 请求信息
     * @return
     */
    @Inner
    @PostMapping(value = "login", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<LoginRsp> login(@RequestBody LoginReq req) {
        return sysUserService.login(req);
    }


    @PostMapping(value = "index", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> index() {
        return R.ok("SUCCEED");
    }


    @PostMapping(value = "removeToken",produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> removeToken(@RequestParam String token) {

        System.out.println("token" + token);
        boolean b = consumerTokenServices.revokeToken(token);

        if (b) {
            return R.ok("SUCCEED");
        }
        return R.failed("退出登录失败");
    }
}
